"""
Case Type   : alter user
Case Name   : 表使用if exists设置用户密码，部分测试点合理报错
Create At   : 2024.09.23
Owner       : @zhanghuan96
Description :
    1、创建兼容b库并查询插件是否加载成功
    2、创建用户并使用if exists修改用户密码
    3、使用if exists修改用户密码(用户不存在/密码不符合要求/password方式修改，密码不加密
    4、使用if exist设置用户密码失效，password方式修改
    5、清理环境
Expect      :
    1、成功，插件查询成功
    2、成功
    3、用户不存在有notice/密码不符合要求报错/密码不加密报错
    4、成功
    5、清理成功
History     :
"""

import os
import unittest

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import macro


class CompatibilityTest01(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.common = Common()
        self.sh_pri = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.db_name = 'db_alter_0018'
        self.u_name = 'u_alter_0018'
        self.pwd1 = 'test@abc2'
        self.pwd2 = 'test@def2'

    def test_create_function(self):
        text = '-----step1:创建兼容b库库并查询插件;expect:成功-----'
        self.log.info(text)
        sql_cmd = (f"drop database if exists {self.db_name};"
                   f"create database {self.db_name} dbcompatibility ='B';")
        self.log.info(sql_cmd)
        sql_res = self.sh_pri.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertEqual(1,
                         sql_res.count(self.constant.CREATE_DATABASE_SUCCESS),
                         '兼容B库创建失败')
        sql_cmd1 = "select extname from pg_extension where extname ='dolphin';"
        self.log.info(sql_cmd1)
        sql_res1 = self.sh_pri.execut_db_sql(sql_cmd1, dbname=self.db_name)
        self.log.info(sql_res1)
        self.assertIn('dolphin', sql_res1, '查询失败')

        text = '-----step2:创建用户并使用if exists修改用户密码;expect:成功-----'
        self.log.info(text)
        sql_cmd2 = (f"drop user if exists {self.u_name};"
                    f"create user {self.u_name} identified by "
                    f"'{macro.COMMON_PASSWD}';"
                    f"alter user if exists {self.u_name} encrypted "
                    f"identified by '{self.pwd1}' "
                    f"replace '{macro.COMMON_PASSWD}';")
        self.log.info(sql_cmd2)
        sql_res2 = self.sh_pri.execut_db_sql(sql_cmd2, dbname=self.db_name)
        self.log.info(sql_res2)
        self.assertIn('CREATE ROLE', sql_res2, '创建失败')
        self.assertIn('ALTER ROLE', sql_res2, '创建失败')

        text = ('-step3:使用if exists修改用户密码(用户不存在/密码不符合要求/'
                'password方式修改，密码不加密/禁用密码);expect:用户不存在有notice/'
                '密码不符合要求报错/密码不加密报错、禁用密码成功-')
        self.log.info(text)
        sql_cmd3 = (f"alter user if exists {self.u_name}_aaa encrypted "
                    f"identified by '{macro.COMMON_PASSWD}';"
                    f"alter user if exists {self.u_name}  encrypted "
                    f"identified by 'test4';"
                    f"alter user if exists {self.u_name} unencrypted "
                    f"password 'test@abc5';"
                    f"alter user if exists {self.u_name} password disable;")
        self.log.info(sql_cmd3)
        sql_res3 = self.sh_pri.execut_db_sql(sql_cmd3, dbname=self.db_name)
        self.log.info(sql_res3)
        self.assertIn('NOTICE:  role "u_alter_0018_aaa" does '
                      'not exist', sql_res3)
        self.assertEqual(2, sql_res3.count('ALTER ROLE'), '创建失败')
        self.assertEqual(2, sql_res3.count('ERROR'), '修改失败')

        text = '-step4:使用if exist设置用户密码失效，password方式修改;expect:成功-'
        self.log.info(text)
        cmd5 = (f"alter user if exists {self.u_name} encrypted "
                f"password '{self.pwd2}';"
                f"alter user if exists  {self.u_name} password '{self.pwd2}_a' "
                f"expired;")
        self.log.info(cmd5)
        res5 = self.sh_pri.execut_db_sql(cmd5, dbname=self.db_name)
        self.log.info(res5)
        self.assertEqual(2, res5.count('ALTER ROLE'), '创建失败')

    def tearDown(self):
        text = '-----step5:清理环境;expect:成功-----'
        self.log.info(text)
        sql_cmd6 = f"drop user if exists {self.u_name};"
        self.log.info(sql_cmd6)
        sql_res6 = self.sh_pri.execut_db_sql(sql_cmd6, dbname=self.db_name)
        self.log.info(sql_res6)
        sql_cmd7 = f"drop database if exists {self.db_name};"
        sql_res7 = self.sh_pri.execut_db_sql(sql_cmd7, dbname='postgres')
        self.log.info(sql_res7)
        self.assertIn('DROP ROLE', sql_res6)
        self.assertEqual(1,
                         sql_res7.count(self.constant.DROP_DATABASE_SUCCESS),
                         '清理环境失败')
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
